package com.jiukuaidao.library.net;

import java.net.HttpURLConnection;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Author : H_Joker
 * Date : 2016/5/12
 * Description :
 */
public class RetroCallBackBytesImpl implements retrofit2.Callback<ResponseBody> {

    private MyRetroCallBytesBack mCallBack;

    public void setCallBack(MyRetroCallBytesBack callBack) {
        mCallBack = callBack;
    }

    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        if (mCallBack != null) {
            if (response.headers().get("graphicMark").equals("1")) {// 图片流返回成功
                if (HttpURLConnection.HTTP_OK == response.code()) {
                    mCallBack.onSuccess(response.body().byteStream());
                } else {
                    mCallBack.onError(null, response.code(), "网络异常，刷新试试~");
                }
            } else {// 图片流返回失败
                mCallBack.onError(null, response.code(), "网络异常，刷新试试~");
            }
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        if (mCallBack != null) {
            mCallBack.onError(t, -1, "网络异常，刷新试试~");
        }
    }
}
